package com.hu.zhcc.shiro.controller.HealthIntervention;

import com.hu.zhcc.common.entity.Page;
import com.hu.zhcc.shiro.entity.bo.HealthIntervention.TemplateManagement;
import com.hu.zhcc.shiro.service.HealthIntervention.TemplateManagementService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/template")
public class TemplateManagementController {
    @Autowired
    private TemplateManagementService service;

    //初始查询
    @GetMapping("")
    public ResponseEntity<Page<TemplateManagement>> findAllList(String tempName , int offset , int limit){
        Map map = new HashMap();
        if(!StringUtils.isBlank(tempName)){
            map.put("tempName",tempName);
        }
        Page<TemplateManagement> templateManagementPage = service.selectAllList(map, offset, limit);
        return ResponseEntity.ok(templateManagementPage);
    }
    //删除
    @DeleteMapping("/{id}")
    public int removeTemplate(@PathVariable("id") int id){
        return service.deleteByPrimaryKey(id);
    }
    //添加
    @PostMapping("")
    public int addTemplate(@RequestBody TemplateManagement management){
        return service.insertSelective(management);
    }

    //根据编号查询
    @GetMapping("/{id}")
    public TemplateManagement TemplateById(@PathVariable("id") int id){
        return service.selectByPrimaryKey(id);
    }

    //添加
    @PutMapping("")
    public int updateTemplate(@RequestBody TemplateManagement management){
        return service.updateByPrimaryKeySelective(management);
    }

    //启用
    @PutMapping("tempTrue/{id}")
    public int updateByTemplateTrue(@PathVariable("id") int id){
        return service.updateByTemplateTrue(id);
    }

    //禁用
    @PutMapping("tempFalse/{id}")
    public int updateByTemplateFalse(@PathVariable("id") int id){
        return service.updateByTemplateFalse(id);
    }

    //下拉框
    @GetMapping("findByStart")
    public ResponseEntity<List<TemplateManagement>> findByTempStart(){
        List<TemplateManagement> templateManagements = service.selectByStart();
        return ResponseEntity.ok(templateManagements);
    }

    @GetMapping("findTempByCrux")
    public ResponseEntity<TemplateManagement> selectTempByCrux(String tempCrux){
        TemplateManagement templateByCrux = service.findTemplateByCrux(tempCrux);
        return ResponseEntity.ok(templateByCrux);
    }
}
